package main

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
)

type Login struct {
	Username string `form:"username" json:"username" binding:"required"`
	Password string `form:"password" json:"password" binding:"required"`
}

func LoginHandler(c *gin.Context) {
	login := &Login{}
	// 最佳实践
	// GET 请求一般使用上面的form标签
	// application/json 这种请求，POST PUT DELETE
	if err := c.ShouldBind(&login); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"msg":  "绑定参数失败" + err.Error(),
			"data": nil,
			"code": 904000,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"msg":  "接口请求成功",
		"data": login,
		"code": 902000,
	})

	c.String(http.StatusOK, fmt.Sprintf("姓名：%s  密码：%s", login.Username, login.Password))
}

func main() {
	c := gin.Default()
	c.POST("/login", LoginHandler)
	c.Run(":8000")
}
